package com.sky.adapter;

import java.util.Objects;


/**
 * 由实现框架集成, 灵活配置
 */
interface JulLogger {
    void fatal(String message, Object... obj);

    void warning(String message, Object... obj);
}


/**
 * 由实现框架集成, 灵活配置
 */
class JulLoggerImpl implements JulLogger {

    @Override
    public void warning(String message, Object... obj) {
        System.out.println(String.format("WARNING  %s  实际调用" + JulLogger.class.getCanonicalName(), message));
    }

    @Override
    public void fatal(String message, Object... obj) {
        System.out.println(String.format("FATAL  %s  实际调用" + JulLogger.class.getCanonicalName(), message));
    }
}

/**
 * 由实现框架集成, 灵活配置
 */
class JulLogAdapter implements Slf4jLogger {
    private JulLogger julLogger;

    JulLogAdapter(JulLogger julLogger) {
        this.julLogger = julLogger;
    }

    @Override
    public void error(String message) {
        Objects.requireNonNull(julLogger);
        julLogger.fatal(message);
    }

    @Override
    public void warn(String message) {
        julLogger.warning(message);
    }
}


interface LogBackLogger {
    void error(String message, Object... obj);

    void warn(String message, Object... obj);
}


/**
 * 由实现框架集成, 灵活配置
 */
class LogBackLoggerImpl implements LogBackLogger {

    @Override
    public void warn(String message, Object... obj) {
        System.out.println(String.format("WARNING  %s  实际调用" + LogBackLogger.class.getCanonicalName(), message));
    }

    @Override
    public void error(String message, Object... obj) {
        System.out.println(String.format("ERROR  %s  实际调用" + LogBackLogger.class.getCanonicalName(), message));
    }
}

/**
 * 由实现框架集成, 灵活配置
 */
class LogBackLoggerAdapter implements Slf4jLogger {
    private LogBackLogger logger;

    LogBackLoggerAdapter(LogBackLogger logger) {
        this.logger = logger;
    }

    @Override
    public void error(String message) {
        Objects.requireNonNull(logger);
        logger.error(message);
    }

    @Override
    public void warn(String message) {
        logger.warn(message);
    }
}



/*--------------------------------------------   抽离   -------------------------------------------------------*/


/**
 * slfj 框架规范, 不许更改
 */
class Slf4jLoggerFactory {

    static String loggerName = "jul";

    static Slf4jLogger getLogger() {
        return bindLogger();
    }

    /**
     * 绑定机制, 这个实际上是反射处理
     */
    static Slf4jLogger bindLogger() {
        if ("jul".equalsIgnoreCase(loggerName)) {
            return new JulLogAdapter(new JulLoggerImpl());
        } else if ("logback".equalsIgnoreCase(loggerName)) {

            return new LogBackLoggerAdapter(new LogBackLoggerImpl());
        } else {
            //omit;
            return null;
        }
    }

    static void setLoggerName(String name) {
        loggerName = name;
    }
}


/**
 * slfj 框架规范, 不许更改
 */
interface Slf4jLogger {
    void error(String message);

    void warn(String message);
}


/**
 * date : 2024-09-06
 * 业务代码 ,  不可更改
 *
 * @author : wushikai
 */
public class Application {
    public static void main(String[] args) {
        Slf4jLoggerFactory.setLoggerName("logback");
        Slf4jLogger logger = Slf4jLoggerFactory.getLogger();
        logger.error("我调用" + logger.getClass().getCanonicalName() + "打印");
        logger.warn("我调用" + logger.getClass().getCanonicalName() + "打印");

    }


}
